﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Models;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using DotNet.Utilities;
using System.IO;
using System.Data;
namespace JinfuyuanProject.Controllers
{
    /*
* 前台模块
* 母子涵
* 2025-04-23
*/
    public class FrontDeskController : Controller
    {
        private readonly IMParturientService _mParturientService;
        private IParturientService _parturient;
        private IPackageService _packageService;
        public FrontDeskController(IMParturientService mParturientService, IParturientService parturient, IPackageService packageService)
        {
            _mParturientService = mParturientService;
            _parturient = parturient;
            _packageService = packageService;
        }
        // GET: FrontDesk
        #region 视图
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Reception()
        {
            List<Package> packages = _packageService.GetList().ToList();
            ViewBag.Packages = packages;
            return View();
        }
        public ActionResult Insert(Parturient parturient, List<Baby> babies, Contract contract, string SalesmanNumbers, string PackagePrice)
        {
            string pName = parturient.ParturientName;
            string pPhone=parturient.ParturientPhone;
            contract.ContractNumber = "jinfuyuan" + DateTime.Now.ToString("yyyyMMddHHmm");
            string templatePath = Server.MapPath("/templates/合同模板.docx");
            string outputPath = Server.MapPath($"/templates/锦福缘母婴湾入住合同_{pName}_{pPhone}.docx");
            contract.ContractNumber = "jinfuyuan" + DateTime.Now.ToString("yyyyMMddHHmm");
            contract.CreateTime = DateTime.Now; ;
            contract.IsDelete = "0";
            contract.SignState = 1;
            contract.Money = decimal.Parse(PackagePrice);
            Staff staff = Session["user"] as Staff;
            contract.SignTime = DateTime.Now;
            contract.LiveTime = DateTime.Now;
            parturient.CreateTime = DateTime.Now;
            parturient.IsDelete = 0;
            parturient.HomeId = 0;//没有入住
            parturient.Reception = staff.Id;
            _parturient.CreateMother(parturient, babies, contract, SalesmanNumbers);//插入数据 孕妇 宝宝 合同
            _parturient.GenerateDocument(parturient, babies, PackagePrice, templatePath, outputPath);//合同生成方法
            return View();
        }
        #endregion
        #region 接口
        public ActionResult SerachParturientName(string keywords)
        {
            return Json(_mParturientService.SearchName(keywords), JsonRequestBehavior.AllowGet);
        }
        //文件下载
        public FileResult DownloadContract(Parturient parturient)
        {
            string pName = parturient.ParturientName;
            string pPhone = parturient.ParturientPhone;
            string outputPath = Server.MapPath($"/templates/锦福缘母婴湾入住合同_{pName}_{pPhone}.docx");
            byte[] fileBytes = System.IO.File.ReadAllBytes(outputPath); // 读取文件为字节数组
            string fileName = Path.GetFileName(outputPath); // 获取文件名
            return File(fileBytes, "application/octet-stream", fileName); // 返回文件并触发下载
        }
        //房间入住时输入孕妇名自动填充
        public ActionResult SerachHomePname(string keywords)
        {

            return Json(_parturient.GetPmessger(keywords), JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}